const formatCurrency = (value, symbol = ',') => {
  if (value && !isNaN(value)) {
    const negative = value < 0 ? '-' : '';
    value = String(Math.abs(value));
    let decimal = '';
    let integer = '';
    if (value.indexOf('.') > -1) {
      [integer, decimal] = value.split('.');
      value = integer;
    }
    const n = value.length % 3;
    if(value > 999) {
      value = negative + (n ? value.substr(0, n) + symbol : '') + value.substr(n).replace(/(\d{3})(?=\d)/g, '$1' + symbol);
    }
    value = decimal ? value + '.' + decimal : value;
  }

  return value;
};

export default formatCurrency;
